/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 供应商管理
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TSupplier;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.DataSupplierModel;
import com.ruipengkj.service.TSupplierService;
import com.ruipengkj.view.base.BasePagePanel;

public class DataSupplierPanel extends BasePagePanel<TSupplier> implements  Runnable{
	
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent vendorTF; //供货商编号
	private JLabelJTextFieldComponent NameTF;//供货商名称
	private JButton queryButton; //查询
	private JButton addButton; //增加
	private JButton updateButton; //修改
	private JButton delButton; //删除
	
	private JTable table;
	private JScrollPane tableJsp;
	private DataSupplierModel dataVendorModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TSupplier> pageView;//翻页主键
	private TSupplierService tSupplierService;//
	private DataSupplierAddOrModifyDialog dataVendorSupplierAddDialog=null;//添加供应
	public DataSupplierPanel() {
		setLayout(new BorderLayout());
		this.init();
		this.initListener();
		tSupplierService=BuildSpringApplication.getInstance().getTSupplierServiceImpl();
	}
	
	private void init() {
		pageView = new PageView<TSupplier>();
		dataVendorModel = new DataSupplierModel();
		table=uiFactory.createRPJTable(dataVendorModel, null, this, 30, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		JPanel gardPanel=uiFactory.createGridJPanel(1, 2);
		JPanel jp1=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		updateButton = new RPJButtonRoundGray("修改(Ctrl+M)");
		delButton = new RPJButtonRoundOrager("删除(Ctrl+D)");
		jp1.add(new JLabel("   "));
		jp1.add(addButton);
		jp1.add(updateButton);
		jp1.add(delButton);
		gardPanel.add(jp1);
		gardPanel.add(pagingComponent);
		
		this.add(initTop(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(gardPanel,BorderLayout.SOUTH);
		//排序
		TableRowSorter<DataSupplierModel> tableRowSorter = new TableRowSorter<DataSupplierModel>(dataVendorModel);  
		table.setRowSorter(tableRowSorter);
	}
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("供应商管理",ImageFactory.loadImage(ImageFactory.FUN_3_ICON_4));
		JPanel top_panel = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		
		vendorTF = new JLabelJTextFieldComponent("供货商编号：", 20);
		NameTF = new JLabelJTextFieldComponent("供货商名称：", 20);
		
		queryButton = new RPJButtonBlue("查询");
		
		top_panel.add(vendorTF);
		top_panel.add(NameTF);
		top_panel.add(queryButton);
		
		queryPanel.addPanel(top_panel);
//		queryPanel.addBoxPanel(top_panel);
		return queryPanel;
	}
	//初始化监听器
	private void initListener() {
		vendorTF.getText().addKeyListener(this);
		NameTF.getText().addKeyListener(this);
		queryButton.addActionListener(this);
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TSupplier tsupplier=new TSupplier();
		tsupplier.setSupplierID(vendorTF.getText().getText().trim());
		tsupplier.setName(NameTF.getText().getText().trim());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TSupplier> pageModel = tSupplierService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tsupplier);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						TabelUtil.clearTableRowSorter(table);
						dataVendorModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						dataVendorModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TSupplier> getPageView() {
		return this.pageView;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询按钮
			this.termQuery(1);
		}else if(e.getSource()==addButton){
			//添加按钮
			dataVendorSupplierAddDialog=new DataSupplierAddOrModifyDialog(null,"添加供应商",true);
			dataVendorSupplierAddDialog.showDialog(null);
			if(dataVendorSupplierAddDialog.isSuccess){
				this.termQuery(1);
			}
		}else if(e.getSource()==updateButton){
			//修改按钮
			this.modifySupplier();
		}else if(e.getSource()==delButton){
			//删除按钮
			this.deleteSupplier();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getSource()==table){
			if(e.getKeyCode()==KeyEvent.VK_DELETE){
				this.deleteSupplier();
			}
		}else if(e.getSource()==vendorTF.getText()||e.getSource()==NameTF.getText()){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();
			}
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==table){
			if(table.getSelectedRowCount()!=0){
				if(e.getClickCount() == 2){
	    			//修改供应商
	    			this.modifySupplier();
	    		}
			}
		}
	}
	//修改供应商
	private void modifySupplier(){
		if(table.getRowCount()!=0){
			int row=table.getSelectedRow();
			if(row!=-1){
				String supplierID=(String)table.getValueAt(row, 0);
				try {
					TSupplier tSupplier=tSupplierService.getByIdService(supplierID);
					dataVendorSupplierAddDialog=new DataSupplierAddOrModifyDialog(null,"修改供应商",true);
					dataVendorSupplierAddDialog.showDialog(tSupplier);
					if(dataVendorSupplierAddDialog.isSuccess){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null,"请选择行在进行修改");
				return;
			}
		}
	}
	 //删除供应商
	private void deleteSupplier(){
		if(table.getRowCount()!=0){
			int[] rows=table.getSelectedRows();
			if(rows != null && rows.length != 0){
				String ok = "";
				if(Util.showConfirmDialog("是否要删除选择记录")==0){
					for (int i = 0,length = rows.length; i < length; ++i) {
						String supplierID=(String)table.getValueAt(rows[i], 0);
						try {
							tSupplierService.deleteService(supplierID);
						} catch (Exception e1) {
							ok += "第"+(i+1)+"条数据删除失败！\n";
							e1.printStackTrace();
						}
					}
					this.termQuery(1);
					if (ok.equals("")) {
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						JOptionPane.showMessageDialog(null, ok);
					}
				}
			}else{
				JOptionPane.showMessageDialog(null,"请选择行在进行删除");
				return;
			}
		}
	}

	@Override
	public void clear() {
		dataVendorModel.clear();
		vendorTF.getText().setText(""); //供货商编号
		NameTF.getText().setText("");//供货商名称
	}
}
